package com.sendi.mobilework.pager.ext;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.sendi.mobilework.R;
import com.sendi.mobilework.adapter.ContactAdapter;
import com.sendi.mobilework.comparator.PinyinComparator;
import com.sendi.mobilework.datamodel.ContactBook;
import com.sendi.mobilework.pager.BasePager;
import com.sendi.mobilework.util.PinyinUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 通讯录页面
 * Created by Administrator on 2017/3/22.
 */

public class ContactBookPager extends BasePager {
    private ListView mListView;
    private List<ContactBook>mList;
    private ContactAdapter mAdapter;
    public ContactBookPager(Activity activity) {
        super(activity);
    }

    @Override
    public void initView() {
        super.initView();
        mTitleText.setText("通讯录");
       View view=View.inflate(mActivity, R.layout.contact_book_layout,null);
        mListView= (ListView) view.findViewById(R.id.lv_contact);
        mFrameLayout.addView(view);
    }


    @Override
    public void initData() {

        mList=new ArrayList<>();
        initContactList(mList);

        filterData(mList);
        Collections.sort(mList,new PinyinComparator());
        mAdapter=new ContactAdapter(mActivity,mList);
        mListView.setAdapter(mAdapter);
    }

    private List<ContactBook> initContactList(List<ContactBook>contactList){

        ContentResolver cr=mActivity.getContentResolver();

        Cursor cursorContactId=cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),new String[]{"contact_id"},null,null,null);

        while (cursorContactId.moveToNext()){

            List<String>list=new ArrayList<>();
            String contactId=cursorContactId.getString(0);
            Cursor cursorData=cr.query(Uri.parse("content://com.android.contacts/data"),new String[]{"data1","mimetype"},
                    "raw_contact_id=?",new String[]{contactId},null);

            while (cursorData.moveToNext()){
                String data=cursorData.getString(0);
                list.add(data);
                Log.i("TEST", "click: "+data);
            }

            contactList.add(new ContactBook(list.get(1),list.get(0)));
        }
        cursorContactId.close();
        return contactList;
    }

    public void filterData(List<ContactBook>contactList){
//        List<ContactBook>mSortList=new ArrayList<>();
        ArrayList<String>indexString =new ArrayList<>();
        for (int i=0;i<contactList.size();i++){

            String pinyin= PinyinUtils.getPinYin(contactList.get(i).getcName());
            String sortString=pinyin.substring(0,1).toUpperCase();
            if (sortString.matches("[A-Z]")){
                contactList.get(i).setSortLetters(sortString.toUpperCase());
                if (!indexString.contains(sortString)){
                    indexString.add(sortString);
                }
            }
        }

    }
}
